﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApCableAttachmentLengthFromStartUpdater
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.ComponentModel;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands;

public class CApCableAttachmentLengthFromStartUpdater
{
  private bool m_isModifyingLengthFromStart;
  private readonly TxNumericEditBoxControl m_lengthFromStartEditBox;
  private readonly ICApFLEXCableAttributeViewModel m_attributeViewModel;
  private readonly CApFLEXCableAttachmentViewModel m_attachmentViewModel;

  public CApCableAttachmentLengthFromStartUpdater(
    TxNumericEditBoxControl lengthFromStartEditBox,
    ICApFLEXCableAttributeViewModel attributeViewModel,
    CApFLEXCableAttachmentViewModel attachmentViewModel)
  {
    this.m_isModifyingLengthFromStart = false;
    this.m_lengthFromStartEditBox = lengthFromStartEditBox;
    this.m_attributeViewModel = attributeViewModel;
    this.m_attachmentViewModel = attachmentViewModel;
    this.m_attributeViewModel.PropertyChanging += new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanging);
    this.m_attributeViewModel.PropertyChanged += new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
    this.m_attachmentViewModel.PropertyChanged += new PropertyChangedEventHandler(this.OnAttachmentPropertyChanged);
  }

  public void SetLengthFromStart(double lengthFromStart)
  {
    if (this.m_isModifyingLengthFromStart || this.m_attachmentViewModel.Location != CApFLEXAttachmentLocation.AlongCable)
      return;
    this.m_attachmentViewModel.SetLengthFromStartNoEventSent(lengthFromStart);
  }

  public void UnregisterEvents()
  {
    this.m_attributeViewModel.PropertyChanging -= new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanging);
    this.m_attributeViewModel.PropertyChanged -= new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
    this.m_attachmentViewModel.PropertyChanged -= new PropertyChangedEventHandler(this.OnAttachmentPropertyChanged);
  }

  private void OnCableLengthChanged()
  {
    if (this.m_attachmentViewModel.Location != CApFLEXAttachmentLocation.End)
      return;
    this.m_lengthFromStartEditBox.Value = this.m_attributeViewModel.Length;
  }

  private void OnAttachmentLocationChanged()
  {
    switch (this.m_attachmentViewModel.Location)
    {
      case CApFLEXAttachmentLocation.AlongCable:
        this.m_lengthFromStartEditBox.Value = this.m_attachmentViewModel.LengthFromStart;
        break;
      case CApFLEXAttachmentLocation.Start:
        this.m_lengthFromStartEditBox.Value = 0.0;
        break;
      case CApFLEXAttachmentLocation.End:
        this.m_lengthFromStartEditBox.Value = this.m_attributeViewModel.Length;
        break;
      default:
        throw new NotImplementedException();
    }
  }

  private void OnAttachmentLengthFromStartChanged()
  {
    if (this.m_attachmentViewModel.Location != CApFLEXAttachmentLocation.AlongCable)
      return;
    this.m_isModifyingLengthFromStart = true;
    this.m_lengthFromStartEditBox.Value = this.m_attachmentViewModel.LengthFromStart;
    this.m_isModifyingLengthFromStart = false;
  }

  private void OnAttributePropertyChange(string propertyName)
  {
    if (!(propertyName == "Length"))
      return;
    this.OnCableLengthChanged();
  }

  private void AttributeViewModel_PropertyChanging(object sender, PropertyChangedEventArgs e)
  {
    this.OnAttributePropertyChange(e.PropertyName);
  }

  private void AttributeViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
    this.OnAttributePropertyChange(e.PropertyName);
  }

  private void OnAttachmentPropertyChanged(object sender, PropertyChangedEventArgs e)
  {
    if (e.PropertyName == "Location")
    {
      this.OnAttachmentLocationChanged();
    }
    else
    {
      if (!(e.PropertyName == "LengthFromStart"))
        return;
      this.OnAttachmentLengthFromStartChanged();
    }
  }
}
